from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
import re

from user.models import Uuser
from user.ser import UuserSer


# Create your views here.
class UuserView(APIView):
    def post(self, request):
        data = {}
        user = request.data.get('account')
        if user == '':
            return Response({"msg": '账号不能为空'})
        password = request.data.get('password')
        ret = re.match('^1[3-9]\d{9}$', user)
        if (ret):
            phone=ret.group()
            a=Uuser.objects.filter(phone=phone).first()
            if a:
                return Response('手机号重复')
            data['phone'] = user
        elif re.match('^[a-zA-Z0-9_]{3,16}@\.com$', user):
            data['email'] = user
        else:
            data['username'] = user
        data['password'] = password
        ser = UuserSer(data=data)
        if ser.is_valid():
            ser.save()
            return Response("注册成功123")
        else:
            return Response('注册失败')
